<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="form" labelWidth="80px">
				<uni-forms-item label="资产编码" name="asset_num" required>				
					<uni-easyinput suffixIcon="scan" v-model="form.asset_num" placeholder="请扫码或输入"
					trim="both" @iconClick="doScan()" @change="getAsset"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="维保类型" name="type" required>
					<uni-data-checkbox v-model="form.type" :localdata="typeOptions"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="维保原由" name="reason" required>
					<uni-easyinput v-model="form.reason" type="textarea" autoHeight placeholder="请输入你的申请原由" />
				</uni-forms-item>
				<uni-forms-item label="维保费用" name="price" required>
					<uni-easyinput v-model="form.price" type="number" placeholder="请输入维保费用" />
				</uni-forms-item>
				<uni-forms-item label="维保说明" name="comment">
					<uni-easyinput v-model="form.comment" type="textarea" autoHeight placeholder="请输入采购说明" />
				</uni-forms-item>				
				<uni-forms-item label="审批人" name="checkUserId" required>
					<uni-data-select v-model="form.checkUserId" :localdata="userOptions" multiple></uni-data-select>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="save">保存并提交</button>
		</view>
	</view>
</template>

<script>
	import { handleTree, parseTime, getDicts	} from '@/api/sys.js'
	import { apiGet, apiPost, apiPut, apiDs	} from '@/api/openapi'

	export default {
		data() {
			return {
				form: {},
				rules: {
					asset_num: {
						rules: [{
							required: true,
							errorMessage: '资产编号不能为空'
						}]
					},
					type: {
						rules: [{
							required: true,
							errorMessage: '维保类型不能为空'
						}]
					},
					reason: {
						rules: [{
							required: true,
							errorMessage: '申请原由不能为空'
						}]
					},
					checkUserId: {
						rules: [{
							required: true,
							errorMessage: '审批人不能为空'
						}]
					}
				},
				typeOptions: [],
				checkUsers: null,
				userOptions:[]
			}
		},
		onLoad(option) {		
			if(option.assetId){
				apiGet({"@table":"asset_info", "id":option.assetId}).then(response => {
					if(response.data == null){
						this.$modal.msgError("未找到资产信息");
						this.form.asset_num = null;
					}else if(response.data.status != '1'){
						this.$modal.msgError("请先将资产归还或标识为闲置状态");
						this.form.asset_num = null;
					}else{
						this.form.asset_id = response.data.id;
						this.form.asset_num = response.data.num;
					}
				})
			}
			apiDs('app_user_select').then(response => {
				this.userOptions = response.data;
			})
			getDicts('asset_maintain_type').then(response => {
				response.data.forEach(e=>{
					e.value = e.dictValue;
					e.text = e.dictLabel;
				})
				this.typeOptions = response.data;	
			});
			this.reset();
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			reset() {
				this.form = {
					id: null,
					asset_id: null,
					asset_num: null,
					type: '1',
					price: 0,
					reason: null,
					comment: null,
					apply_status: '2',
					apply_time:'@now()',
					check_status: '1',
					checkUserId: null
				};
			},			
			// 扫码操作
			doScan(){
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function (res) {
						this.form.asset_num = res.result;
						this.getAsset();
					}
				});
			},
			getAsset() {
				apiGet({"@table":"asset_info", "num":this.form.asset_num}).then(response => {
					if(response.data == null){
						this.$modal.msgError("未找到资产信息");
						this.form.asset_num = null;
					}else if(response.data.status != '1'){
						this.$modal.msgError("请先将资产归还或标识为闲置状态");
						this.form.asset_num = null;
					}else{
						this.form.asset_id = response.data.id;
					}
				})
			},
			save(ref) {
				this.$refs.form.validate().then(res => {
					let data = {"@tags": ['asset_maintain', 'asset_flow']};
					data['asset_maintain'] = this.form;
					
					let userName = null;
					for (var j = 0; j < this.userOptions.length; j++) {
						if (this.userOptions[j].value == this.form.checkUserId) {
							userName = this.userOptions[j].text;
						}
					}
					
					data['asset_flow'] = {
						"data_id":"@asset_maintain.id",
						"flow_model":'asset_maintain',
						"check_user_id": this.form.checkUserId,
						"check_user_name": userName
					};
					apiPost(data).then(response => {
					  this.$modal.msgSuccess("提交成功");
					  this.reset();
					});			
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>